package com.tracy.life.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.tracy.life.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class WebFragment extends Fragment {

    private final static String KEY = "url";

    public static WebFragment newInstance(String url) {
        WebFragment fragment = new WebFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY, url);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_web, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String url = null;
        if (getArguments() != null) {
            url = (String) getArguments().get(KEY);
        }

        WebView webView = (WebView) view.findViewById(R.id.wv_desc);
        webView.loadDataWithBaseURL(null, url, "text/html", "utf_8", null);
        //设置一个新的浏览器开启，不设置则会使用手机的浏览器打开网页
        webView.setWebViewClient(new WebViewClient());
        //获取到设置
        WebSettings settings = webView.getSettings();
        //设置可以加载JavaScript
        settings.setJavaScriptEnabled(true);
        //设置缓存，下次加载页面从缓存中加载
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

    }
}
